package com.example.photography.Activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSONObject;
import com.example.photography.R;
import com.example.photography.adapter.ViewPagerAdapter;
import com.example.photography.bean.FocusBean;
import com.example.photography.common.BaseActivity;
import com.example.photography.fragment.focus.Fans_fragment;
import com.example.photography.fragment.focus.Focus_fragment;
import com.example.photography.util.UIUtils;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.photography.Activity.Store_Activity.getJson;

public class Focus_Activity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tb_item)
    TabLayout tbItem;
    @BindView(R.id.vp_invest)
    ViewPager vpInvest;
    private List<Fragment> fragmentList = new ArrayList<>();
    private FocusBean focusBean;
    private int sign = 0;
    private ViewPagerAdapter viewPagerAdapter;

    @Override
    protected void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("data");
        if (bundle != null) {
            sign = bundle.getInt("sign", 0);

        }
        String json = getJson(this, "focus.json");
        if (!json.isEmpty()) {
            focusBean = JSONObject.parseObject(json, FocusBean.class);
        }
        initView();
    }

    private void initView() {
        tvTitle.setText("粉丝与关注");
        initFragments();
        initListen();
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), UIUtils.getStringArr(R.array.focus_tab), fragmentList);
        vpInvest.setAdapter(viewPagerAdapter);
        vpInvest.setCurrentItem(sign);//设置viewpager的初始化位置
        tbItem.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //将TabLayout绑定ViewPager
        tbItem.setupWithViewPager(vpInvest);
        //TabLayout平分空间
        tbItem.setTabMode(TabLayout.MODE_FIXED);
        tbItem.setTabGravity(TabLayout.GRAVITY_FILL);
    }

    private void initListen() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                removeCurrentActivity();
            }
        });

    }

    private void initFragments() {
        //添加到集合中
        fragmentList.add(new Focus_fragment(focusBean.getData().getRelations(), focusBean.getData().getFocuss()));
        fragmentList.add(new Fans_fragment(focusBean.getData().getFanss()));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_focus;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
